package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.OrgApplyWuHan;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 * 安心分期&武汉全量
 * @ Author WJB
 */
@ApiPlan(productId = "287", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5WuHan implements ApiOrgPhoneMd5ConvertHandler {

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        return MapUtils.newHashMap(newMap -> {
            newMap.put("mobileMd5", apiDataExchangeParameter.getPhoneMd5());
            newMap.put("workCity", apiDataExchangeParameter.getCity());
        });
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        // 创建进件实体类
        OrgApplyWuHan applyWuHan = new OrgApplyWuHan();
        // 创建拓展工具类
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        // 客户手机号
        applyWuHan.setMobile(apiApplyParameter.getPhone());
        // 客户姓名
        applyWuHan.setRealName(apiApplyParameter.getUserName());
        // 年龄
        applyWuHan.setAge(apiFieldConverter.getAge());
        // 性别（男/女）
        String sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", "1")
                .when(apiApplyParameter.getSex(), "1", "2")
                .end();
        applyWuHan.setGender(sex);
        // 城市
        applyWuHan.setWorkCity(apiApplyParameter.getCity());
        //  职业
        String career = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", "10")
                .when(apiApplyParameter.getCareer(), "2", "20")
                .when(apiApplyParameter.getCareer(), "3", "30")
                .when(apiApplyParameter.getCareer(), "4", "10")
                .end();
        applyWuHan.setJob(career);
        // 是否有车
        String carInfo = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", "0")
                .when(apiApplyParameter.getCarInfo(), "2", "1")
                .when(apiApplyParameter.getCarInfo(), "3", "2")
                .when(apiApplyParameter.getCarInfo(), "4", "1")
                .end("0");
        applyWuHan.setCarProperty(carInfo);
        // 是否有房
        String realEstateInfo = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", "0")
                .when(apiApplyParameter.getRealEstateInfo(), "2", "1")
                .when(apiApplyParameter.getRealEstateInfo(), "3", "2")
                .when(apiApplyParameter.getRealEstateInfo(), "4", "1")
                .when(apiApplyParameter.getRealEstateInfo(), "5", "1")
                .when(apiApplyParameter.getRealEstateInfo(), "6", "1")
                .end("0");
        applyWuHan.setHouseProperty(realEstateInfo);
        // 是否有公积金
        String providentFund = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", "0")
                .when(apiApplyParameter.getProvidentFund(), "2", "1")
                .when(apiApplyParameter.getProvidentFund(), "3", "2")
                .when(apiApplyParameter.getProvidentFund(), "4", "1")
                .end("0");
        applyWuHan.setHousingFund(providentFund);
        // 是否有表单
        String insurancePolicy = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", "0")
                .when(apiApplyParameter.getInsurancePolicy(), "2", "1")
                .when(apiApplyParameter.getInsurancePolicy(), "3", "2")
                .when(apiApplyParameter.getInsurancePolicy(), "4", "1")
                .end("0");
        applyWuHan.setInsurance(insurancePolicy);
        // 是否有社保
        String socialSecurity = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", "0")
                .when(apiApplyParameter.getSocialSecurity(), "2", "1")
                .when(apiApplyParameter.getSocialSecurity(), "3", "2")
                .when(apiApplyParameter.getSocialSecurity(), "4", "1")
                .end("0");
        applyWuHan.setSocialSecurity(socialSecurity);
        // 需求金额（元）
        String money = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1","1")
                .when(apiApplyParameter.getLoanAmount(), "2","3")
                .when(apiApplyParameter.getLoanAmount(), "3", "4")
                .when(apiApplyParameter.getLoanAmount(), "4", "4")
                .end();
        applyWuHan.setApplyLimit(money);
        // 逾期
        String overdue = ChooseEq.create()
                .when(apiApplyParameter.getOverdue(), "1", "0")
                .end("1");
        applyWuHan.setCreditInvestigation(overdue);
        // 芝麻分
        String zhima = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", "4")
                .when(apiApplyParameter.getSesameCredit(), "2", "5")
                .when(apiApplyParameter.getSesameCredit(), "3", "2")
                .when(apiApplyParameter.getSesameCredit(), "4", "3")
                .end("0");
        applyWuHan.setSesameCredit(zhima);
        return applyWuHan;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
